
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver import ActionChains,Keys
import time
class Base():
    # 初始化
    def __init__(self,driver):
        self.driver=driver
    # 获取网址
    def get(self,url):
        self.driver.get(url)
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as f:
            # 时间戳
            t=time.strftime("%Y%m%d-%H%M%S",time.localtime())
            # 截图
            self.driver.save_screenshot(f'{t}.png')
            print(f)
    # def save_screenshot(self):
    #     # 时间戳
    #     t = time.strftime("%Y%m%d-%H%M%S", time.localtime())
    #     # 截图
    #     self.driver.save_screenshot(f'{t}.png')
    # 点击
    def click(self,loc):
        self.find_element(loc).click()
    # 输入
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 断言
    def text(self,loc):
        return self.find_element(loc).text
    # 滚动
    def execute_script(self,info):
        self.driver.execute_script(f'window.scrollTo(0,{info})')
    # alert弹窗
    def alertT(self):
        alert = self.driver.switch_to.alert
        alert.accept()
    # 进iframe框架
    def jin_iframe(self,loc):
        self.driver.switch_to.frame(self.driver.find_element(loc))
    # 退iframe框架
    def tui_iframe(self):
        self.driver.switch_to.default_content()
    # 切换窗口
    def window_handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 鼠标单击
    def ActionChains_click(self,loc):
        ActionChains(self.driver).click(self.find_element(loc)).perform()
    # 鼠标双击
    def ActionChains_double_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    # 键盘事件_回车
    def key_enter(self,loc):
        self.send_keys(loc,Keys.ENTER)
    # 键盘事件_左
    def key_LEFT(self,loc):
        self.send_keys(loc,Keys.LEFT)
    # 键盘事件_右
    def key_RIGHT(self, loc):
        self.send_keys(loc, Keys.RIGHT)
    # 键盘事件_空格
    def key_SPACE(self, loc):
        self.send_keys(loc, Keys.SPACE)
    # 键盘事件_退格
    def key_BACKSPACE(self, loc):
        self.send_keys(loc, Keys.BACKSPACE)
    # 浏览器——退出
    def quite(self):
        self.driver.quit()
    # 浏览器——刷新
    def refresh(self):
        self.driver.refresh()
    # 浏览器窗口最大化
    def maximize_window(self):
        self.driver.maximize_window()
    # 下拉框
    def select_value(self,loc,info):
        select=Select(self.driver.find_element(loc))
        select.select_by_value(info)
    # 下拉框
    def select_index(self, loc, info):
        select = Select(self.driver.find_element(loc))
        select.select_by_index(info)
    # 隐式等待
    def implicitly_wait(self,info):
        self.driver.implicitly_wait(info)